package com.googlecode.cchlib.apps.duplicatefiles;

import java.io.File;
import java.io.Serializable;

/**
 *
 */
public final class KeyFileState
    implements  Serializable,
                Comparable<KeyFileState>
{
    private static final long serialVersionUID = 1L;
    private final String  key;
    private final File    file;
    private String  path;
    private boolean selectedToDelete;
    private Integer depth;

    /**
     * File object
     *
     * @param key
     * @param file
     */
    public KeyFileState(String key, File file)
    {
        this.key  = key;
        this.file = file;
        this.selectedToDelete = false;
     }

    @Override
    public String toString()
    {
        return this.file.getPath();
    }

    /**
     * @return the key
     */
    public String getKey()
    {
        return key;
    }

    /**
     * @return the file
     */
    public File getFile()
    {
        return file;
    }

    /**
     * Returns depth of file
     * @return depth of file
     */
    public int getDepth()
    {
        if( depth == null ) {
            int  d = 0;
            File f = file.getParentFile();

            while( f != null ) {
                f = f.getParentFile();
                d++;
                }
            depth = Integer.valueOf( d );
            }
        return depth.intValue();
    }

    public boolean isInDirectory( String dirPath )
    {
        if( path == null ) {
            path = file.getPath();
            }

        return path.startsWith( dirPath );
    }

    /**
     * @return the selectedToDelete
     */
    public final boolean isSelectedToDelete()
    {
        return selectedToDelete;
    }

    /**
     * @param selectedToDelete the selectedToDelete to set
     */
    public final void setSelectedToDelete( boolean selectedToDelete )
    {
        this.selectedToDelete = selectedToDelete;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31; // $codepro.audit.disable numericLiterals
        int result = 1;
        result = (prime * result) + ((file == null) ? 0 : file.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals( Object obj )
    {
        if( this == obj ) {
            return true;
        }
        if( obj == null ) {
            return false;
        }
        if( !(obj instanceof KeyFileState) ) {
            return false;
        }
        KeyFileState other = (KeyFileState)obj;
        if( file == null ) {
            if( other.file != null ) {
                return false;
            }
        } else if( !file.equals( other.file ) ) {
            return false;
        }
        return true;
    }

    @Override
    public int compareTo( KeyFileState kfs )
    {
        return file.compareTo( kfs.file );
    }

    public String getFileNameWithoutExtention()
    {
        final String name = getFileNameWithExtention();

        if( name.indexOf( 0 ) == '.' ) {
            // File start with DOT, so it's a hidden file (or system file) - no extention
            return name;
            }

        final int index = name.lastIndexOf( '.' );

        if( index < 0 ) {
            // No extention
            return name;
            }

        return name.substring( 0, index );
    }

    public String getFileNameWithExtention()
    {
        return getFile().getName();
    }
}
